package com.example.service.impl;

import cn.hutool.core.io.FileUtil;
import com.example.context.BaseContext;
import com.example.entity.Homework;
import com.example.entity.Script;
import com.example.mapper.HomeworkMapper;
import com.example.mapper.ScriptMapper;
import com.example.result.PageResult;
import com.example.service.IScriptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.utils.BaseException;
import com.example.utils.CommonControllers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;



/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 陈昌吉
 * @since 2024-04-16
 */
@Service
public class ScriptServiceImpl extends ServiceImpl<ScriptMapper, Script> implements IScriptService {
    @Autowired
    private CommonControllers commonControllers;
    @Autowired
    private ScriptMapper scriptMapper;
    @Autowired
    private HomeworkMapper homeworkMapper;


    /**
     * Scrip上传文件到oss和插入到数据库中
     *上传后返回对应的值
     * 判断上传时间是在结束时间之前还是之后
     * @param file
     * @param hid
     * @throws IOException
     */

    public List<Script> insert(MultipartFile file, Integer hid) {
        List<Homework> work = homeworkMapper.getWork();
        for (int j = 0; j < work.size(); j++) {
            if (work.get(j).getHid().equals(hid) && (LocalDateTime.now()).isAfter(work.get(j).getEndtime())){
                throw new BaseException("作业时间已结束，不能在提交",501);
            }
        }

        String originalFilename = file.getOriginalFilename();//获取文件原始名称
        String s = FileUtil.mainName(originalFilename);//获取文件前面名不含后缀
        String s1 = FileUtil.extName(originalFilename);//获取文件后缀名

        String upload = String.valueOf(commonControllers.upload(file));//获取返回的url

        Script build = Script.builder()
                .scriptFile(upload)
                .loginId(BaseContext.getCurrentId())
                .name(originalFilename)
                .uploadtime(LocalDateTime.now())
                .hid(hid)
                .build();

        Integer currentId = BaseContext.getCurrentId();
        List<Script> scripts = scriptMapper.sllectScript(currentId,hid);
        for (int i=0;i<scripts.size();i++){
            if (scripts.get(i).getName().equals(originalFilename)){
                throw new BaseException("该文件已存在，不能重复上传",500);
            }
        }
        int insert = scriptMapper.insert(build);
        List<Script> script = scriptMapper.sllectScript(currentId,hid);
        return script;

    }

    /**
     * 获取所有Scrip
     * @return
     */

    @Override
    public List<Script> getAll(Integer hid) {
        Script a = new Script();
        a.setLoginId( BaseContext.getCurrentId());
        a.setHid(hid);
        return  scriptMapper.sllectScripts(a);
    }

    /**
     * 删除oss文件和数据的url
     * @param script
     * @return
     */

    @Transactional
    public String deleteFile(Script script) {
        String delete = commonControllers.delete(script.getScriptFile());
        scriptMapper.deleteScriptUrl(script);
        return delete;
    }

    /**
     * 获取作业状态
     * @param hids
     * @return
     */

    @Override
    public   LinkedHashSet<Script> getScriptState(ArrayList<Integer> hids) {
        Script script = new Script();
        Integer currentId = BaseContext.getCurrentId();
        List<Script> state= scriptMapper.getState(currentId,hids);
        LinkedHashSet<Script> hashSet = new LinkedHashSet<>(state);
        return  hashSet ;

    }

    /**
     * 审核员处获取所有的作业和分页
     *
     * @param script
     * @return
     */
    @Override
    public PageResult getScrip(Script script) {
        PageHelper.startPage(script.getPageIndex(),script.getPageSize());
        Page<Script> page=  scriptMapper.seleectgetScrip(script);
        return new PageResult(page.getTotal(),page.getResult());
    }
    /**
     * 审核对提交的作业进行审核
     * @param script
     * @return
     */
    @Override
    public void updateState(Script script) {
        System.out.println(script);
        Integer i = scriptMapper.updateState(script);

    }
}
